package com.honour.controller.interfaced;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.SortInManager;
import com.honour.service.interfaced.UserInManager;
import com.honour.service.interfaced.impl.AdvertisementInService;
import com.honour.service.interfaced.impl.EnergyInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.DateUtil;
import com.system.util.MapDistance;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;



/**
 @Function   首页特权KTV分类
 @Author zhaozhenhua
 @Date   2017年11月17日
*/

@Controller
@RequestMapping(value="/appsktvin")
public class SortKTVInController extends BaseController{
	
	@Resource(name="sortinService")
	private SortInManager sortinService;
	
	@Resource(name="userinService")
	private UserInManager userinService;
	
	@Resource(name="energyInService")
	private EnergyInService energyInService;
	
	@Resource(name="advertisementInService")
	private AdvertisementInService advertisementInService;

	
	/**
	 * 获得首页特权KTV分类列表
	 * @Author zhaozhenhua
	 * @date   2017年11月17日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/sktv")
	@ResponseBody
	public Object sHotel(){
		logBefore(logger, "获得首页中KTV分类列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTKTV", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					//获得某家商店的综合评价
					List<PageData> list = sortinService.getSortKTVList(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getDistance(data);
						data.put("distance",  m.get("distance"));
						//将地址修改为标准字符串
						data.put("KTVADDRESS", data.get("KTVADDRESS").toString().replace(",", ""));
					}
					//添加广告
					pd.put("RELATED_ID", "3");
					List<PageData> ad = advertisementInService.getAdByPosition(pd);
					map.put("ad", ad);
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
			
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * KTV店铺添加用户评价接口
	 * @Author zhaozhenhua
	 * @date   2017年11月9日
	 * @return
	 */
	@RequestMapping(value="/sktvaddeva")
	@ResponseBody
	public Object sHotelAddEva(){
		logBefore(logger, "KTV店铺添加用户评价接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SKTVADDEVA", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sKTVAddEva", pd)){	//检查参数
				pd.put("KTV_ID", this.getSnowId());
				pd.put("EVALUATEDATE", DateUtil.getDay());
				pd.put("EVALUATECOINT",Double.parseDouble((String) pd.get("EVALUATECOINT")));
				//用户是否匿名评价判断
				if (pd.get("ISSHOW").equals("1")) {
					pd.put("NICKNAME", "******");
				}else{
					pd.put("NICKNAME", sortinService.getUserNickName(pd).get("NICKNAME"));
				}
				sortinService.sKTVEvaAdd(pd);
				//每当店铺有新评价，则更新评价的分数和评价数量
				double d = sortinService.sKTVEvaMean(pd);
				Integer i = sortinService.sKTVEvaCount(pd);
				pd.put("KTVEVALUATE", d);
				pd.put("KTVEVACOUNT", i);
				sortinService.sKTVUpdEva(pd);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 获得某家KTV的综合评价
	 * @Author zhaozhenhua
	 * @date   2017年11月9日
	 * @return
	 */
	@RequestMapping(value="/sktvmeaneva")
	@ResponseBody
	public Object sHotelMeanEva(){
		logBefore(logger, "KTV店铺综合评价分数接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SKTVMEANEVA", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sKTVMeanEva", pd)){	//检查参数
					//获得某家商店的综合评价
					double d = sortinService.sKTVEvaMean(pd);
					map.put("pd", d);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得某家KTV的评价数量
	 * @Author zhaozhenhua
	 * @date   2017年11月9日
	 * @return
	 */
	@RequestMapping(value="/sktvcounteva")
	@ResponseBody
	public Object sHotelCountEva(){
		logBefore(logger, "某家KTV的评价数量接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SKTVCOUNTEVA", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sKTVMeanEva", pd)){	//检查参数
					//获得某家商店的综合评价
					Integer d = sortinService.sKTVEvaCount(pd);
					map.put("pd", d);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得某家KTV的具体服务接口
	 * @Author zhaozhenhua
	 * @date   2017年11月10日
	 * @return
	 */
	@RequestMapping(value="/sktvserv")
	@ResponseBody
	public Object sFoodServ(){
		logBefore(logger, "获得某家KTV的具体服务接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SKTVSERV", pd.getString("FKEY"))){	//检验请求key值是否合法
					List<PageData> list = sortinService.sFoodServ(pd);
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取KTV详情列表接口
	 * @Author zhaozhenhua
	 * @date   2017年11月18日
	 * @return
	 */
	@RequestMapping(value="/ktvdetaillist")
	@ResponseBody
	public Object personal(){
		logBefore(logger, "获取KTV详情列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("KTVDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = sortinService.getKTVDetailList(pd);
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 获得用户名下所有KTV订单列表	(待付款，待使用，待评价)
	 * @Author zhaozhenhua
	 * @date   2017年11月18日
	 * @return
	 */
	@RequestMapping(value="/ktvorderalllist")
	@ResponseBody
	public Object getAllHotelOrderList(){
		logBefore(logger, "获得用户名下所有KTV订单列表接口(待付款，待使用，待评价)");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("ALLKTVORDER", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("allBarOrder", pd)){	//检查参数
					List<PageData> list = sortinService.getAllKTVOrderList(pd);
					map.put("pd", list);
					//返回01为创建成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 生成KTV订单接口（未产生交易）
	 * @Author zhaozhenhua
	 * @date   2017年11月18日
	 * @return
	 */
	@RequestMapping(value="/shipktvorder")
	@ResponseBody
	public Object shipHotelOrder(){
		logBefore(logger, "生成KTV订单接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();		
		String result = "00";
		PageData po = new PageData();
		po = this.getPageData();
		try{
			if(Tools.checkKey("SHIPKTVORDER", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipKTVOrder", pd)){	//检查参数
					String KTVORDER_ID = this.getSnowId();
					pd.put("KTVORDER_ID", KTVORDER_ID);
					//调用工具类生成酒店订单号
					String getnum = ToolsUtil.GetHotelOrderID(pd.getString("HONOURUSER_ID"));
					pd.put("ORDERNUMBER",getnum);
					pd.put("ORDERTIME", ToolsUtil.getStringDate());
					//将酒店订单状态改为待付款
					pd.put("ORDERSTATUS", "0040001");
					//添加相对应的酒店订单
					sortinService.addKTVOrder(pd);
					//添加相对应的用户订单中间信息
					po.put("USERORDER_ID", this.getSnowId());
					po.put("ORDERTYPE", "3");  //1代表美食2代表酒店3代表ktv
					po.put("HONOURUSER_ID", pd.getString("HONOURUSER_ID"));
					po.put("SORTTYPE_ID", KTVORDER_ID);
					sortinService.addUserOrder(po);
					//返回01为创建成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 修改KTV订单状态(改为支付成功)
	 * @Author zhaozhenhua
	 * @date   2017年11月18日
	 * @return
	 */
	@RequestMapping(value="/shipktvstaupd")
	@ResponseBody
	public Object shipHotelOrderStaUpd(){
		logBefore(logger, "修改KTV订单状态接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPKTVORDERUPD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipKTVOrderStaUpd", pd)){	//检查参数
				//对酒店订单状态进行修改
				pd.put("ORDERPAYTIME", ToolsUtil.getStringDate());
				sortinService.updKTVOrderStatus(pd);
				//订单状态为0040003时,获取购物分
				if("0040003".equals(pd.getString("ORDERSTATUS"))){
					//获取参与分值
					Integer score = energyInService.getShareScore(pd);
					//获取原始分值
					PageData energyScore = userinService.getEnergyScore(pd);
					Integer SHOPSCORE=energyScore.getInterger("SHOPSCORE")+score;
					pd.put("SHOPSCORE", SHOPSCORE);
					userinService.updEnergyScore(pd);
				}
				//返回01为修改成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * KTV详情综合接口
	 * @Author 于泓磊
	 * @date   2017年11月22日
	 * @return
	 */
	@RequestMapping(value="/ktvsyn")
	@ResponseBody
	public Object hotelSyn(){
		logBefore(logger, "ktv详情综合接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("KTVSYN", pd.getString("FKEY"))){	//检验请求key值是否合法
				pd=sortinService.ktvSyn(pd);
				pd.remove("count(DISTINCT SORTKTV_ID)");
				 //调用工具类将数据库属性变成数组传到前台
				ToolsUtil.putImgstoArray(pd, "KTVDETAILSIMGS");
				//将地址修改为标准字符串
				pd.put("KTVADDRESS", pd.get("KTVADDRESS").toString().replace(",", ""));
				map.put("pd", pd);
				//返回01为修改成功并返回
				result =  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *	根据经纬度计算距离
	 * @param args
	 * @throws Exception
	 */
	public Object getDistance(PageData pd){
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success",distance="";
		try {
			PageData p = sortinService.ktvDistance(pd);
			distance  = MapDistance.getDistance(p.getString("KTVLONGITUDE"),p.getString("KTVLATITUDE"),p.getString("USERLONGITUDE"),p.getString("USERLATITUDE"));
		} catch (Exception e) {
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		map.put("distance", distance);			//距离
		return AppUtil.returnObject(new PageData(), map);
	}
	

	
	/**
	 * 某KTV评价列表接口
	 * @Author 于泓磊
	 * @date   2017年11月25日
	 * @return
	 */
	@RequestMapping(value="/ktvevalist")
	@ResponseBody
	public Object ktvEvalist(){
		logBefore(logger, "某KTV评价列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("KTVEVA", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sKTVMeanEva", pd)){	//检查参数
				List<PageData> list = sortinService.ktvEvaList(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
				//返回01为修改成功并返回
				result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 随机产生KTV列表
	 * @Author zhaozhenhua
	 * @date   2017年11月29日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/sktvlistrand")
	@ResponseBody
	public Object sHotelListRand(){
		logBefore(logger, "随机产生KTV列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTKTVRAND", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					//随机产生KTV列表
					List<PageData> list = sortinService.getKTVListByRand(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getDistance(data);
						data.put("distance",  m.get("distance"));
						//将地址修改为标准字符串
						data.put("KTVADDRESS", data.get("KTVADDRESS").toString().replace(",", ""));
					}
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
			
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 根据KTV评价分数，价格，距离升序或者降序排序（品牌，类型）
	 * @Author zhaozhenhua
	 * @date   2017年11月30日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/sktvlistsequence")
	@ResponseBody
	public Object sKTVListSequence(){
		logBefore(logger, "根据KTV评价分数，价格升序或者降序排序（品牌，类型）");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTKTVSEQUENCE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sSequence", pd)){	//检查参数
					List<PageData> list = sortinService.getKTVListBySequence(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getDistance(data);
						data.put("distance",  m.get("distance"));
						//将地址修改为标准字符串
						data.put("KTVADDRESS", data.get("KTVADDRESS").toString().replace(",", ""));
					}
					 if(pd.get("SEQUENCE")==null||pd.get("SEQUENCE")==""){
                    	//按距离对酒店排序
    					Collections.sort(list, new Comparator<PageData>(){
    						@Override
    						public int compare(PageData o1, PageData o2) {
    							String dis1=(String) o1.get("distance");
    							String dis2=(String) o2.get("distance");
    							int intDis1 = Double.valueOf(dis1).intValue();
    							int intDis2 = Double.valueOf(dis2).intValue();
    							// 按照距离进行升序排列  
    							if(intDis1>intDis2){
    								return 1;
    							}
    							if(intDis1==intDis2){
    								return 0;
    							}
    							return -1;
    						}

    					});
					}
					
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
			
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	

}
